package com.zone.demo.controller;

import com.zone.demo.domain.entity.Myuser;
import com.zone.demo.service.IMyuserService;
import com.zone.demo.util.JsonResult;
import com.zone.demo.util.ResultCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MMinbo
 * @since 2019-10-25
 */
@RestController
@RequestMapping("/test/myuser")
public class MyuserController {

    @Autowired
	private IMyuserService myuserService;

	@ApiOperation(value = "获得用户列表", notes = "", httpMethod = "GET")
	@GetMapping(value = "/listUser")
	public JsonResult listUser() {
		// 框架自带的方法
		List<Myuser> list = this.myuserService.list(null);
		return new JsonResult(ResultCode.SUCCESS, list);
	}
	@ApiOperation(value = "根据用户名获得用户记录", notes = "名称不能为空", httpMethod = "GET")
	@ApiImplicitParam(dataType = "string", name = "name", value = "用户名", required = true)
	@GetMapping(value = "/getUserByName")
	public JsonResult getUserByName(String name) {
		// 自定义的方法
		Myuser myuser = this.myuserService.getUserByName(name);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}
	@GetMapping(value = "/getUserById")
	public JsonResult getUserById(Long id) {
		// 自定义的方法
		Myuser myuser = this.myuserService.getUserById(id);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}
	@PostMapping(value = "/addUser")
	public JsonResult addUser(Long id, String name, int age , String email) {
		// 自定义的方法
		this.myuserService.addUser(id,name,age,email);
		Myuser myuser = this.myuserService.getUserById(id);
		return new JsonResult(ResultCode.SUCCESS, myuser);

	}
	@GetMapping(value = "/deleteUserByName")
	public JsonResult deleteUserByName(String name) {
		// 自定义的方法
		this.myuserService.deleteUserByName(name);
		List<Myuser> myuser = this.myuserService.list(null);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}
	@PostMapping(value = "/updateUserById")
	public JsonResult updateUserById(long id,String name,int age,String email) {
		// 自定义的方法
		this.myuserService.updateUserById(id,name,age,email);
		Myuser myuser = this.myuserService.getUserById(id);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}

	@GetMapping(value = "/add")
	public double add(Integer initial, int loop) {
		// 自定义的方法
		return this.myuserService.add(initial,loop);
	}
	@GetMapping(value = "/dataToString")
	public String dateToString(String data) {
		return this.myuserService.dataToString(data);
	}

	@GetMapping(value = "/sub")
	public double sub(Integer frist,Integer second) {
		return this.myuserService.sub(frist,second);
	}
	@GetMapping(value = "/ifTest")
	public String ifTest(Integer frist, Integer second) {
		return this.myuserService.ifTest(frist,second);
	}



}
